const serveActive = 'production';
const path = require('path')
const CompressionPlugin = require('compression-webpack-plugin');
// 国外免费（jsdelivr）CDN https://www.jsdelivr.com/
const cdn = {
    css: [],
    js: [
        'https://cdn.jsdelivr.net/npm/vue@2.6.11/dist/vue.min.js',
        'https://cdn.jsdelivr.net/npm/vuex@3.4.0/dist/vuex.min.js',
        'https://cdn.jsdelivr.net/npm/vue-router@3.2.0/dist/vue-router.min.js',
        'https://cdn.jsdelivr.net/npm/axios@0.21.4/dist/axios.min.js',
        'https://cdn.jsdelivr.net/npm/ant-design-vue@1.7.6/dist/antd.min.js'
    ]
}
function resolve(dir) {
    return path.join(__dirname, dir)
}
module.exports = {

    css: {
        loaderOptions: {
            less: {
                modifyVars: {
                    'primary-color': '#1890FF',
                    'link-color': '#1890FF',
                    'border-radius-base': '4px',
                },
                javascriptEnabled: true,
            }
        }
    },

    devServer: {
        open: true,
        host: '0.0.0.0',
        port: 3000,
        https: false,
        proxy: {
            '/gecba': {
                target: "http://localhost:8080/",
                ws: true,
                changeOrigin: true,
                pathRewrite: {
                    "^/gecba": "/gecba"
                }
            }
        }
    },

    /** 路径配置 */
    chainWebpack: (config) => {
      config.resolve.alias
          .set('@',resolve('src'))
          .set('@api',resolve('src/api'))
          .set('@assets',resolve('src/assets'))
          .set('@comp',resolve('src/components'))
          .set('@views',resolve('src/views'))
          .set('@layout',resolve('src/layout'))
          .set('@static',resolve('src/static'))
          .set('@store',resolve('src/store'))
          .set('@mobile',resolve('/src/modules/mobile'))
        config.resolve.symlinks(true)
    },

    // 打包时不生成.map文件 避免看到源码
    productionSourceMap: false,
    // 部署优化
    configureWebpack: {
        // 使用CDN
        externals: process.env.NODE_ENV === serveActive ? {
            vue: 'Vue',
            axios: 'axios',
            'vue-router': 'VueRouter',
            vuex: 'Vuex',
            'ant-design-vue': 'antd',
        } : {},
        plugins: [
            // GZIP压缩
            new CompressionPlugin({
                test: /\.js$|\.html$|\.css/, // 匹配文件
                threshold: 10240 // 对超过10k文件压缩
            })
        ]
    },
    pages: {
        index: {
            entry: 'src/main.js',
            template: 'public/index.html',
            filename: 'index.html',
            title: 'gecba',
            chunks: ['chunk-vendors', 'chunk-common', 'index'],
            cdn: process.env.NODE_ENV === serveActive ?  cdn : {}
        }
    },

    lintOnSave: false,

}
